# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql
import pymysql.cursors
from twisted.enterprise import adbapi


class ToutiaoPipeline(object):
    def process_item(self, item, spider):
        return item


class twistedMysqlPipeline(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, setting):
        dbparm = dict(
            host=setting['MYSQL_HOST'],
            user=setting['MYSQL_USER'],
            passwd=setting['MYSQL_PASSWD'],
            db=setting['MYSQL_DB'],
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor
        )
        dbpool = adbapi.ConnectionPool('pymysql', **dbparm)
        return cls(dbpool)

    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self.do_insert, item)

    def do_insert(self, cursor, item):
        insert_data_sql = "insert ignore into toutiao_data VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', {})"
        insert_label_sql = "insert ignore into label values('{}', '{}')"
        key = ['group_id', 'title', 'source', 'chinese_tag',
               'link', 'abstract', 'article_genre', 'comments_count']
        cursor.execute(insert_data_sql.format(*[item[i] for i in key]))
        for label in item['label']:
            cursor.execute(insert_label_sql.format(item.get('group_id'), label))

